package com.zs.letcode.illustration_of_algorithm;

import java.util.*;

/**
 * 剑指 Offer 34. 二叉树中和为某一值的路径
 * 输入一棵二叉树和一个整数，打印出二叉树中节点值的和为输入整数的所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。
 * <p>
 * <p>
 * 示例:
 * 给定如下二叉树，以及目标和target = 22，
 * <p>
 * 5
 * / \
 * 4   8
 * /   / \
 * 11  13  4
 * /  \    / \
 * 7    2  5   1
 * 返回:
 * <p>
 * [
 * [5,4,11,2],
 * [5,8,4,5]
 * ]
 * <p>
 * 提示：
 * <p>
 * 节点总数 <= 10000
 * 注意：本题与主站 113题相同：https://leetcode-cn.com/problems/path-sum-ii/
 * <p>
 * 相关标签
 * 树
 * 深度优先搜索
 * 回溯
 * 二叉树
 * <p>
 * 作者：Krahets
 * 链接：https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5dy6pt/
 * 来源：力扣（LeetCode）
 * 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
 *
 * @author madison
 * @description
 * @date 2021/8/18 11:04
 */
public class Chapter30 {
    public static void main(String[] args) {

    }

    /**
     * Definition for a binary tree node.
     */
    private class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private class Solution {
        /**
         * 方法一：深度优先搜索
         */
        LinkedList<List<Integer>> res = new LinkedList<>();
        LinkedList<Integer> path = new LinkedList<>();

        public List<List<Integer>> pathSum(TreeNode root, int target) {
            recur(root, target);
            return res;
        }

        private void recur(TreeNode root, int target) {
            if (root == null) {
                return;
            }
            path.add(root.val);
            target -= root.val;
            if (target == 0 && root.left == null && root.right == null) {
                res.add(new LinkedList<>(path));
            }
            recur(root.left, target);
            recur(root.right, target);
            path.removeLast();
        }

        /**
         * 方法二：广度优先搜索
         */
        List<List<Integer>> ret = new LinkedList<List<Integer>>();
        Map<TreeNode, TreeNode> map = new HashMap<TreeNode, TreeNode>();

        public List<List<Integer>> pathSum1(TreeNode root, int target) {
            if (root == null) {
                return ret;
            }
            Queue<TreeNode> queueNode = new LinkedList<>();
            Queue<Integer> queueSum = new LinkedList<>();
            queueNode.offer(root);
            queueSum.offer(0);

            while (!queueNode.isEmpty()) {
                TreeNode node = queueNode.poll();
                int rec = queueSum.poll() + node.val;
                if (node.left == null && node.right == null) {
                    if (rec == target) {
                        getPath(node);
                    }
                } else {
                    if (node.left != null) {
                        map.put(node.left, node);
                        queueNode.offer(node.left);
                        queueSum.offer(rec);
                    }
                    if (node.right != null) {
                        map.put(node.right, node);
                        queueNode.offer(node.right);
                        queueSum.offer(rec);
                    }
                }
            }
            return ret;
        }

        private void getPath(TreeNode node) {
            List<Integer> tmp = new LinkedList<>();
            while (node != null) {
                tmp.add(node.val);
                node = map.get(node);
            }
            Collections.reverse(tmp);
            ret.add(new LinkedList<>(tmp));
        }
    }
}
